package org.jeecg.modules.register.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.exProject.entity.ExProject;
import org.jeecg.modules.register.entity.SubjectLegal;
import org.jeecg.modules.register.entity.SubjectMemberLevel;
import org.jeecg.modules.register.mapper.SubjectLegalMapper;
import org.jeecg.modules.register.mapper.SubjectMemberLevelMapper;
import org.jeecg.modules.register.service.ISubjectMemberLevelService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Description: subject_member_level
 * @Author: jeecg-boot
 * @Date:   2024-11-10
 * @Version: V1.0
 */
@Service
public class SubjectMemberLevelServiceImpl extends ServiceImpl<SubjectMemberLevelMapper, SubjectMemberLevel> implements ISubjectMemberLevelService {

    @Resource
    private SubjectMemberLevelMapper subjectMemberLevelMapper;

    @Resource
    private SubjectLegalMapper subjectLegalMapper;

    @Override
    public IPage<SubjectMemberLevel> selChecklist(Page<SubjectMemberLevel> page, SubjectMemberLevel subjectMemberLevel) {

        return page.setRecords(subjectMemberLevelMapper.selectMemberCheckList(page, subjectMemberLevel));


    }

    @Override
    public Result<String> checkById(SubjectMemberLevel subjectMemberLevel) {
        if(null == subjectMemberLevel.getId()){
            return Result.error("ID不能为空！");
        }

        //0 待审核 1 起效 2 失效
        if(null == subjectMemberLevel.getMemberState()){
            return Result.error("审核结果不能为空");
        }

        subjectMemberLevel.setCheckTime(new Date());

        if("1".equals(subjectMemberLevel.getMemberState())){
            subjectMemberLevelMapper.updateCheckSucc(subjectMemberLevel);
            subjectMemberLevel.setMemberState("2");
            subjectLegalMapper.updateLagelLevelInfoByCode(subjectMemberLevel);
            SubjectLegal subjectLegal = new SubjectLegal();

            subjectLegal.setLegalCode(subjectMemberLevel.getLegalCode());
        }else{
            subjectMemberLevelMapper.updateById(subjectMemberLevel);
            subjectMemberLevel.setMemberState("3");
            subjectLegalMapper.updateLagelLevelInfoByCodeState(subjectMemberLevel);

        }

        return Result.OK("审核成功！");
    }

    @Override
    public void saveLevel(SubjectMemberLevel subjectMemberLevel) {
        subjectMemberLevelMapper.insert(subjectMemberLevel);
        subjectMemberLevel.setMemberState("1");
        subjectLegalMapper.updateLagelLevelInfoByCodeState(subjectMemberLevel);
    }
}
